/*
 * FileCopierTest.java
 *
 * Created on 15 April 2007, 11:06
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.cardcopier.file;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jmock.Mock;
import org.jmock.cglib.MockObjectTestCase;

/**
 *
 * @author Mark
 */
public class FileCopierTest extends MockObjectTestCase {
    
    private static final FileLocation SOURCE = new CaseInsensitiveFileLocation(new File("test-data/source"), new String[] {"cr2"});
    private static final File SOURCE_FILE_1 = new File("test-data/source", "file-1.cr2");
    private static final File SOURCE_FILE_2 = new File("test-data/source", "file-2.cr2");
    private static final File SOURCE_FILE_3 = new File("test-data/source", "file-3.cr2");
    
    private static final File DESTINATION = new File("build", "FileCopierTest");
    private static final File DESTINATION_FILE_1 = new File(DESTINATION, "file-1.cr2");
    private static final File DESTINATION_FILE_2 = new File(DESTINATION, "file-2.cr2");
    private static final File DESTINATION_FILE_3 = new File(DESTINATION, "file-3.cr2");
    
    /** Creates a new instance of FileCopierTest */
    public FileCopierTest() {
    }
 
    public void testCanCopyFiles() throws IOException {
        List<FileState> sourceFiles = new ArrayList<FileState>();
        sourceFiles.add(new FileState(SOURCE_FILE_1, DESTINATION_FILE_1));

        Mock fileUtilsMock = mock(FileUtils.class);
        fileUtilsMock.expects(once()).method("copyFile").with(eq(SOURCE_FILE_1), eq(DESTINATION_FILE_1));
        
        new FileCopier((FileUtils)fileUtilsMock.proxy(), false).copyFiles(sourceFiles);
        verify();
    }
    
    public void testWillNotifyListenersOfCopyStartAndFinish() throws IOException {
        Mock fileUtilsMock = mock(FileUtils.class);
        fileUtilsMock.stubs();
        FileCopier fileCopier = new FileCopier((FileUtils)fileUtilsMock.proxy(), false);

        List<FileState> sourceFiles = new ArrayList<FileState>();
        sourceFiles.add(new FileState(SOURCE_FILE_1, DESTINATION_FILE_1));
        sourceFiles.add(new FileState(SOURCE_FILE_2, DESTINATION_FILE_2));
        sourceFiles.add(new FileState(SOURCE_FILE_3, DESTINATION_FILE_3));

        Mock fileCopierListenerMock = mock(FileCopierListener.class);
        fileCopierListenerMock.expects(once()).method("fileCopyStarted").with(eq(SOURCE_FILE_1), eq(DESTINATION_FILE_1));
        fileCopierListenerMock.expects(once()).method("fileCopyStarted").with(eq(SOURCE_FILE_2), eq(DESTINATION_FILE_2));
        fileCopierListenerMock.expects(once()).method("fileCopyStarted").with(eq(SOURCE_FILE_3), eq(DESTINATION_FILE_3));
        fileCopierListenerMock.expects(once()).method("fileCopyFinished").with(eq(SOURCE_FILE_1), eq(DESTINATION_FILE_1));
        fileCopierListenerMock.expects(once()).method("fileCopyFinished").with(eq(SOURCE_FILE_2), eq(DESTINATION_FILE_2));
        fileCopierListenerMock.expects(once()).method("fileCopyFinished").with(eq(SOURCE_FILE_3), eq(DESTINATION_FILE_3));
        fileCopier.addFileCopierListener((FileCopierListener) fileCopierListenerMock.proxy());
        
        fileCopier.copyFiles(sourceFiles);
    }

//    public void testCopyIsCancellable() throws IOException {
//        Object lock = new Object();
//        DummyFileUtils dummyFileUtils = new DummyFileUtils(lock);
//        final FileCopier fileCopier = new FileCopier(dummyFileUtils, SOURCE, DESTINATION);
//
//        Mock fileCopierListenerMock = mock(FileCopierListener.class);
//        fileCopierListenerMock.expects(once()).method("fileCopyStarted").with(eq(SOURCE_FILE_1), eq(DESTINATION_FILE_1));
//        fileCopierListenerMock.expects(once()).method("fileCopyFinished").with(eq(SOURCE_FILE_1), eq(DESTINATION_FILE_1));
//        fileCopier.addFileCopierListener((FileCopierListener) fileCopierListenerMock.proxy());
//
//        new Thread(new Runnable() {
//            public void run() {
//                try {
//                    fileCopier.copyFiles();
//                } catch (IOException ex) {
//                    ex.printStackTrace();
//                }
//            }
//        }).start();
//        
//        synchronized (lock) {
//            try {
//                lock.wait();
//            } catch (InterruptedException ex) {
//            }
//        }
//        fileCopier.cancelCopy();
//        dummyFileUtils.finishCopy();
//        
//    }
//    
//    private static class DummyFileUtils extends FileUtils {
//        
//        private final Object lock;
//        public DummyFileUtils(Object lock) {
//            this.lock = lock;
//        }
//        
//        public synchronized void copyFile(File source, File destination) throws IOException {
//            synchronized (lock) {
//                lock.notifyAll();
//            }
//            
//            try {
//                this.wait();
//            } catch (InterruptedException ex) {
//            }
//        }
//        
//        public synchronized void finishCopy() {
//            this.notifyAll();
//        }
//
//    }

}
